package com.regiee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.regiee.dto.SetmealDto;
import com.regiee.entity.Setmeal;
import com.regiee.entity.SetmealDish;
import com.regiee.mapper.SetmealMapper;
import com.regiee.service.SetmealDishService;
import com.regiee.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal，执行插入操作
        this.save(setmealDto);
        //保存关联关联关系
        List<SetmealDish> list=setmealDto.getSetmealDishes();
        for(SetmealDish sd:list){
            sd.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(list);
    }

    @Override
    @Transactional
    public SetmealDto getSetmealById(Long id) {
        Setmeal st=this.getById(id);
        SetmealDto sdto=new SetmealDto();
        LambdaQueryWrapper<SetmealDish> wrapper=new LambdaQueryWrapper<SetmealDish>()
                .eq(SetmealDish::getDishId,id);
        List<SetmealDish> list=setmealDishService.list(wrapper);
        sdto.setSetmealDishes(list);
        BeanUtils.copyProperties(st,sdto);
        return sdto;
    }
}
